package com.cynteka.cyfoman.core.collection;

import com.cynteka.cyfoman.core.Task;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.sound.midi.MidiMessage;
import java.util.*;
import java.util.logging.Logger;

/**
 * User: Sergey
 * Date: 03.08.11
 * Time: 17:05
 */
public class MailScanner implements Task {
    private String host;
    private String username;
    private String password;
    private int port = 110;
    private Properties properties = new Properties();
    private String protocol = "pop3";
    private static final String INBOX = "inbox";
    private static Logger logger = Logger.getLogger(MailScanner.class.getName());

    public CollectionRegistry getCollectionRegistry() {
        return collectionRegistry;
    }

    public void setCollectionRegistry(CollectionRegistry collectionRegistry) {
        this.collectionRegistry = collectionRegistry;
    }

    public String getCollectionId() {
        return collectionId;
    }

    public void setCollectionId(String collectionId) {
        this.collectionId = collectionId;
    }

    private CollectionRegistry collectionRegistry;
    private String collectionId;

    @Override
    public boolean isActive() {
        return true;
    }

    @Override
    public void run() {
        List<Message> lstMessages = new ArrayList<Message>();
        try {
            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties);

            // Get a Store object that implements the specified protocol.
            Store store = session.getStore(protocol);

            //Connect to the current host using the specified username and password.
            store.connect(host, username, password);

            //Create a Folder object corresponding to the given name.
            Folder folder = store.getFolder(INBOX);

            Flags flags = folder.getPermanentFlags();
            System.out.println(flags);

            // Open the Folder.
            folder.open(Folder.READ_ONLY);

            // Get the messages from the server
            Message[] messages = folder.getMessages();
            //System.out.println("Found : " + messages.length);


            // Display message.
                //*
            for (int i = 0; i < messages.length; i++) {
                Message message = messages[i];

                if(message instanceof MimeMessage) {
                    MimeMessage mimeMessage = (MimeMessage) message;
                    // System.out.println(mimeMessage.getMessageID());
                }
                // System.out.println("------------ Message " + (i + 1) + " ------------");
                // Here's the big change...

                String from = InternetAddress.toString(messages[i].getFrom());
                if (from != null) {
                    // System.out.println("From: " + from);
                }
                /*
                String replyTo = InternetAddress.toString(
                        messages[i].getReplyTo());
                if (replyTo != null) {
                    System.out.println("Reply-to: " + replyTo);
                }
                String to = InternetAddress.toString(
                        messages[i].getRecipients(Message.RecipientType.TO));
                if (to != null) {
                    System.out.println("To: " + to);
                }
                String cc = InternetAddress.toString(
                        messages[i].getRecipients(Message.RecipientType.CC));
                if (cc != null) {
                    System.out.println("Cc: " + cc);
                }
                String bcc = InternetAddress.toString(
                        messages[i].getRecipients(Message.RecipientType.BCC));
                if (bcc != null) {
                    System.out.println("Bcc: " + to);
                }
                String subject = messages[i].getSubject();
                if (subject != null) {
                    System.out.println("Subject: " + subject);
                }
                Date sent = messages[i].getSentDate();
                if (sent != null) {
                    System.out.println("Sent: " + sent);
                }
                Date received = messages[i].getReceivedDate();
                if (received != null) {
                    System.out.println("Received: " + received);
                }

//                System.out.println();
//*/

            }

            lstMessages.addAll(Arrays.asList(messages));
            getMemoryCollection().setItems(lstMessages);

            folder.close(true);
            store.close();
        } catch (MessagingException me) {

        }

    }

    protected MemoryCollection getMemoryCollection() {
        return (MemoryCollection) collectionRegistry.getCollection(collectionId);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

}
